﻿/*
Copyright (c) 2009 Vratislav Cermak (vratacermak@gmail.com)
 
This file is part of TeachMe Project. See http://code.google.com/p/teach-me for more information.
 
TeachMe is an open source project. Using of TeachMe is free of charge.
It is distributed under GNU General Public License version 3 (GPLv3).
GPLv3 can be found at http://www.gnu.org/licenses/gpl.html.
 
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE.  See the license for more details.
*/

using System.Collections.Generic;
using System.Windows.Forms;
using TeachMe.Application.Forms;

namespace TeachMe.Application
{
    // TODO: i intend to make this class central point of all forms related operations

    public class FormsManager
    {
        private readonly Root _root;

        public FormsManager(Root root)
        {
            _root = root;
        }

        private readonly List<PseudoModalForm> _forms = new List<PseudoModalForm>();

        public void Add(PseudoModalForm form)
        {
            _forms.Add(form);
        }

        public void Remove(PseudoModalForm form)
        {
            _forms.Remove(form);
        }

        public int Count
        {
            get { return _forms.Count;}
        }

        public T Get<T>() where T : PseudoModalForm
        {
            foreach (PseudoModalForm form in _forms)
            {
                if (form is T)
                {
                    return form as T;
                }
            }

            return null;
        }

        public bool IsDoingOptions()
        {
            foreach (PseudoModalForm form in _forms)
            {
                if (form is OptionsForm)
                {
                    return true;
                }
            }

            return false;
        }

        public bool IsEditingEntries()
        {
            foreach (PseudoModalForm form in _forms)
            {
                if (form is EntriesEditorForm || form is AdvancedEntryEditorForm)
                {
                    return true;
                }
            }

            return false;
        }

        public void ShowForms()
        {
            foreach (PseudoModalForm form in _forms)
            {
                form.Activate();
            }
        }

        public void ShowDictionariesManager()
        {
            if (Get<DictionariesManagerForm>() != null)
            {
                Get<DictionariesManagerForm>().Activate();
            }
            else if (_forms.Count == 0)
            {
                DictionariesManagerForm form = new DictionariesManagerForm(_root);
                Add(form);
                form.Show();
                form.Closed += delegate
                {
                    Remove(form);
                };
            }
        }

        public void ShowOptionsForm()
        {
            if (Get<OptionsForm>() != null)
            {
                Get<OptionsForm>().Activate();
            }
            else if (_forms.Count == 0)
            {
                OptionsForm form = new OptionsForm(_root);
                Add(form);
                form.Show();
                form.Closed += delegate
                {
                    Remove(form);
                };
            }
        }

        public void ShowCreateEntryDialog()
        {
            if (Get<AdvancedEntryEditorForm>() != null)
            {
                Get<AdvancedEntryEditorForm>().Activate();
            }
            else if (_forms.Count == 0)
            {
                List<EditedEntryDescriptor> entries = new List<EditedEntryDescriptor>();
                entries.Add(EditedEntryDescriptor.CreateNew());

                AdvancedEntryEditorForm form = new AdvancedEntryEditorForm(_root, entries, null);
                Add(form);
                form.Show();
                form.Closed += delegate
                                   {
                                       if (form.DialogResult == DialogResult.OK && form.Dictionary != null)
                                       {
                                           form.Dictionary.UpdateEntries(entries);
                                       }

                                       Remove(form);
                                   };
            }
        }

        public void ShowAboutForm()
        {
            if (Get<AboutForm>() != null)
            {
                Get<AboutForm>().Activate();
            }
            else if (_forms.Count == 0)
            {
                AboutForm form = new AboutForm(_root);
                Add(form);
                form.Show();
                form.Closed += delegate
                {
                    Remove(form);
                };
            }
        }

        public void ShowMemoForm(PseudoModalForm parent, string caption, string text)
        {
            MemoForm form = Get<MemoForm>();

            if (form != null)
            {
                form.Activate();
            }
            else
            {
                form = new MemoForm();
                Add(form);
                form.PseudoParent = parent;
                form.SetCaption(caption);
                form.SetText(text);
                form.Show();
                form.Closed += delegate
                {
                    Remove(form);
                };
            }
        }
    }
}
